﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

namespace WebClientTest
{
    class Program
    {
        static void Main(string[] args)
        {
            WebClientInvoke invoke = new WebClientInvoke();
            //invoke.OpenRead();
            //invoke.DownLoadFile();

            //invoke.DownLoadData();

            //invoke.DownLoadString();
            //invoke.OpenWrite();

            //invoke.UpLoadData();

            //invoke.UpLoadFile();

            invoke.UpLoadString();
        }
    }

    public class WebClientInvoke
    {

        #region 下载

        /// <summary>
        /// 简单读取html
        /// </summary>
        public void OpenRead()
        {
            WebClient client = new WebClient();
            client.UseDefaultCredentials = true;
            client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
            var data = client.OpenRead(
                "https://www.baidu.com/s?wd=LOL&rsv_spt=1&rsv_iqid=0xf844dcda000742ef&issp=1&f=8&rsv_bp=1&rsv_idx=2&ie=utf-8&rqlang=cn&tn=baiduhome_pg&rsv_enter=0&oq=webclient&rsv_t=d1b1H0WUZRrVzCQkKANtOkE1UwukfwSGXZF9K%2BfIlGgVTPyAuDafZJq7pwjd3jub9SdA&inputT=2486&rsv_pq=8a90957800040b81&rsv_sug3=12&rsv_sug1=11&rsv_sug7=100&rsv_sug4=2486");
            var reader = new StreamReader(data);
            Console.WriteLine(reader.ReadToEnd());
            reader.Close();
        }

        /// <summary>
        /// 下载网页
        /// </summary>
        public void DownLoadFile()
        {
            WebClient client = new WebClient();

            client.DownloadFile("http://lol.uuu9.com/lpl/", DateTime.Now.ToLongDateString() + ".html");
        }

        /// <summary>
        /// 以数据形式获取html内容
        /// </summary>
        public void DownLoadData()
        {
            WebClient client = new WebClient();
            string uri = "http://lol.uuu9.com/lpl/";
            var htmlBytes = client.DownloadData(uri);
            var result = Encoding.ASCII.GetString(htmlBytes);
            Console.WriteLine(result);
        }

        /// <summary>
        /// 以字符串形式获取html内容
        /// </summary>
        public void DownLoadString()
        {
            WebClient client = new WebClient();
            string uri = "http://lol.uuu9.com/lpl/";
            var htmlStr = client.DownloadString(uri);
            Console.WriteLine(htmlStr);
        }

        #endregion

        #region 上传

        /// <summary>
        /// 上传文件(流)
        /// </summary>
        public void OpenWrite()
        {
            WebClient client = new WebClient();
            string uri = "d://test.txt";
            string content = "test str";
            var postData = Encoding.ASCII.GetBytes(content);
            var write= client.OpenWrite(uri);
            write.Write(postData,0, postData.Length);
            write.Close();
        }

        /// <summary>
        /// 通过字节数组方式上传
        /// </summary>
        public void UpLoadData()
        {
            WebClient client = new WebClient();
            string uri = "d://test.txt";
            string content = "test str from uploaddata";
            var postData = Encoding.ASCII.GetBytes(content);
            client.UploadData(uri, postData);
        }

        /// <summary>
        /// 通过文件上传
        /// </summary>
        public void UpLoadFile()
        {
            WebClient client = new WebClient();
            string uri = "d://file2.txt";
            string fileUrl = "d://file1.txt";
            var result= client.UploadFile(uri, fileUrl);
        }

        /// <summary>
        /// 通过字符串上传
        /// </summary>
        public void UpLoadString()
        {
            WebClient client = new WebClient();
            string uri = "d://test.txt";
            string content = "test str from uploadstring";
            var result= client.UploadString(uri, content);
        }

        #endregion
    }
}
